package cn.edu.buaa.sei.jdat.vt.widgets;

import cn.edu.buaa.sei.jdat.model.Element;
import cn.edu.buaa.sei.jdat.model.Field;
import cn.edu.buaa.sei.jdat.model.IModifier;
import cn.edu.buaa.sei.jdat.model.Interface;
import cn.edu.buaa.sei.jdat.model.Jar;
import cn.edu.buaa.sei.jdat.model.Method;
import cn.edu.buaa.sei.jdat.model.Package;
import cn.edu.buaa.sei.jdat.vt.res.Resources;

import com.cocotingo.snail.GraphicsX;
import com.cocotingo.snail.MouseEvent;
import com.cocotingo.snail.View;
import com.cocotingo.snail.text.RichTextBlock;
import com.cocotingo.snail.text.RichTextView;

public class JavaElementListView extends ListView {
	
	public class Item extends View {
		
		private final Element element;
		private final RichTextView textView;
		private boolean selected;

		public Item(final Element element) {
			super(0, 0, 0, 30);
			setBackgroundColor(null);
			this.element = element;
			selected = false;
			addSubview(textView = new RichTextView(0, 0, 0, 0) {
				{
					setBackgroundColor(null);
					// TODO: make colorful text label
					RichTextBlock textBlock = new RichTextBlock(element.getName());
					textBlock.setFont(Resources.FONT_MONO);
					textBlock.setColor(Resources.COLOR_TEXT_DEFAULT);
					setText(new RichTextBlock[] {textBlock});
				}
			});
		}
		
		public Element getElement() {
			return element;
		}
		
		@Override
		public View setSize(final int width, final int height) {
			super.setSize(width, height);
			if (textView != null) {
				textView.setPosition(30, 7);
				textView.setWidth(width - 30);
				textView.setBottom(0);
			}
			return this;
		}
		
		@Override
		protected void repaintView(GraphicsX g) {
			// draw icon
			if (element instanceof Jar) {
				g.drawImage(Resources.ICON_JAR, 0, 0);
			} else if (element instanceof Package) {
				g.drawImage(Resources.ICON_PACKAGE, 0, 0);
			} else if (element instanceof cn.edu.buaa.sei.jdat.model.Class) {
				g.drawImage(Resources.ICON_CLASS, 0, 0);
			} else if (element instanceof Interface) {
				g.drawImage(Resources.ICON_INTERFACE, 0, 0);
			} else if (element instanceof Method) {
				g.drawImage(Resources.ICON_METHOD, 0, 0); // TODO: use constructor icon
			} else if (element instanceof Field) {
				g.drawImage(Resources.ICON_FIELD, 0, 0);
			}
			
			if (element instanceof IModifier) {
				IModifier modifier = (IModifier) element;
				if (modifier.isAbstract()) g.drawImage(Resources.ICON_ABSTRACT, 0, 0);
				if (modifier.isFinal()) g.drawImage(Resources.ICON_FINAL, 0, 0);
				if (modifier.isStatic()) g.drawImage(Resources.ICON_STATIC, 0, 0);
				if (modifier.getAccessModifiers() == IModifier.PROTECTED) {
					g.drawImage(Resources.ICON_PROTECTED, 0, 0);
				} else if (modifier.getAccessModifiers() == IModifier.DEFAULT) {
					g.drawImage(Resources.ICON_DEFAULT, 0, 0);
				} else if (modifier.getAccessModifiers() == IModifier.PRIVATE) {
					g.drawImage(Resources.ICON_PRIVATE, 0, 0);
				}
			}
		}
		
		public boolean isSelected() {
			return selected;
		}
		
		public void setSelected(boolean selected) {
			setBackgroundColor(selected ? Resources.COLOR_TEXT_SHADOW : null);
			if (!this.selected && selected) {
				this.selected = true;
				for (View item : JavaElementListView.this.getContentView().getSubviews()) {
					if (item != this) {
						((Item) item).setSelected(false);
					}
				}
				JavaElementListView.this.selectedItemChanged(this);
			} else if (this.selected && !selected) {
				this.selected = false;
			}
		}
		
		@Override
		public void removeFromSuperView() {
			super.removeFromSuperView();
			if (selected) {
				JavaElementListView.this.selectedItemChanged(null);
			}
		}
		
		@Override
		protected void postMousePressed(MouseEvent e) {
			e.handle();
			setSelected(true);
		}
		
	}

	public JavaElementListView(int left, int top, int width, int height) {
		super(left, top, width, height);
	}
	
	public void addElement(Element element) {
		getContentView().addSubview(new Item(element));
		layout();
	}
	
	public void removeAllElements() {
		getContentView().removeAllSubviews();
		layout();
	}
	
	public Item getItem(int index) {
		return (Item) getContentView().getSubview(index);
	}
	
	public int indexOf(Element element) {
		for (View view : getContentView().getSubviews()) {
			Item item = (Item) view;
			if (item.getElement() == element) {
				return item.getIndex();
			}
		}
		return -1;
	}
	
	protected void selectedItemChanged(Item item) {}

}
